package org.hxyjs.service.impl;

import org.hxyjs.dao.NewsDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.News;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.NewsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("NewsService")
public class NewsServiceImpl implements NewsService {
    @Resource
    NewsDao newsDao;
    @Override
    public DTO<News> getAllNewss()  throws Exception{
        List<News> newss = newsDao.getAllNewss();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(newss).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage(),newss);
    }

    @Override
    public DTO<News> deleteNewsByNeno(String neno)  throws Exception{
        Integer result = newsDao.deleteNewsByNeno(neno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEADMINSUCCESS:MessageEnum.DELETEADMINFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<News> getNewsByNeno(String neno)  throws Exception{
        News newss = newsDao.getNewsByNeno(neno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(newss).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage(),newss);
    }

    @Override
    public DTO<News> getNewsByPage(PageInfo pageInfo) throws Exception {
        List<News> news = newsDao.getNewsByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(news).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage(),news,pageInfo);
    }

    @Override
    public DTO<News> updateNewsByNeno(News news) throws Exception {
        Integer result = newsDao.updateNewsByNeno(news);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEADMINSUCCESS:MessageEnum.UPDATEADMINFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<News> addNews(News news) throws Exception {
        Integer result = newsDao.addNewsByNeno(news);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.ADDADMINSUCCESS:MessageEnum.ADDADMINFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage());
    }

    /**
     * 学生选课成功 学生成绩加1分，从选课信息中可以获得学生的学号就能查询出学生原来的成绩
     * @param sc
     * @return
     * @throws Exception
     */

}
